package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesEquipmentExamineRpcService;
import com.hunttown.mes.rpc.domain.MesEquipmentExamineDTO;
import com.hunttown.mes.rpc.domain.query.MesEquipmentExamineDTOQuery;
import com.hunttown.mes.service.MesEquipmentExamineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesEquipmentExamineConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:25:45
 */
@Service
public class MesEquipmentExamineRpcServiceImpl implements MesEquipmentExamineRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesEquipmentExamineRpcServiceImpl.class);

    private final MesEquipmentExamineService mesEquipmentExamineService;

    @Autowired
    public MesEquipmentExamineRpcServiceImpl(MesEquipmentExamineService mesEquipmentExamineService) {
        this.mesEquipmentExamineService = mesEquipmentExamineService;
    }

    @Override
    public MesEquipmentExamineDTO insert(MesEquipmentExamineDTO mesEquipmentExamineDTO) {
        try {
            return convert(mesEquipmentExamineService.insert(convert(mesEquipmentExamineDTO)));
        } catch (Exception e) {
            logger.error("MesEquipmentExamine create failed.", e);
            throw new RuntimeException("MesEquipmentExamine create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesEquipmentExamineDTO mesEquipmentExamineDTO) {
        try {
            return mesEquipmentExamineService.updateInfoById(convert(mesEquipmentExamineDTO));
        } catch (Exception e) {
            logger.error("MesEquipmentExamine update by id failed.", e);
            throw new RuntimeException("MesEquipmentExamine update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesEquipmentExamineDTOQuery mesEquipmentExamineDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentExamineDTOQuery.getW_id() != null && mesEquipmentExamineDTOQuery.getW_id() > 0) {
                query.put("w_id", mesEquipmentExamineDTOQuery.getW_id());
            }

            if (mesEquipmentExamineDTOQuery.getIds() != null && !mesEquipmentExamineDTOQuery.getIds().equals("")) {
                query.put("ids", mesEquipmentExamineDTOQuery.getIds());
            }

            if (mesEquipmentExamineDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesEquipmentExamineDTOQuery.getStopFlag());
            }

            if (mesEquipmentExamineDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentExamineDTOQuery.getDeleteFlag());
            }

            return mesEquipmentExamineService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesEquipmentExamine update by query failed.", e);
            throw new RuntimeException("MesEquipmentExamine update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentExamineDTO selectById(Integer id) {
        try {
            return convert(mesEquipmentExamineService.selectById(id));
        } catch (Exception e) {
            logger.error("MesEquipmentExamine select by id failed.", e);
            throw new RuntimeException("MesEquipmentExamine select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentExamineDTO selectObjByQuery(MesEquipmentExamineDTOQuery mesEquipmentExamineDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentExamineDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesEquipmentExamineDTOQuery.getStopFlag());
            }

            if (mesEquipmentExamineDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentExamineDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesEquipmentExamineDTOQuery.getOrderBy())) {
                query.put("orderBy", mesEquipmentExamineDTOQuery.getOrderBy());
            }

            return convert(mesEquipmentExamineService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesEquipmentExamine select obj by query failed.", e);
            throw new RuntimeException("MesEquipmentExamine select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesEquipmentExamineDTO> getListForPage(MesEquipmentExamineDTOQuery mesEquipmentExamineDTOQuery) {
        Query query = Query.page(mesEquipmentExamineDTOQuery.getPn(), mesEquipmentExamineDTOQuery.getPageSize());

        if (mesEquipmentExamineDTOQuery.getId() != null && mesEquipmentExamineDTOQuery.getId() > 0) {
            query.put("id", mesEquipmentExamineDTOQuery.getId());
        }

        if (mesEquipmentExamineDTOQuery.getIds() != null && !mesEquipmentExamineDTOQuery.getIds().equals("")) {
            query.put("ids", mesEquipmentExamineDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineDTOQuery.getLikeName())) {
            query.put("likeName", mesEquipmentExamineDTOQuery.getLikeName());
        }

        if (mesEquipmentExamineDTOQuery.getEquipId() != null && mesEquipmentExamineDTOQuery.getEquipId() > 0) {
            query.put("equipId", mesEquipmentExamineDTOQuery.getEquipId());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineDTOQuery.getShowDate())) {
            query.put("showDate", mesEquipmentExamineDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineDTOQuery.getStartDate())) {
            query.put("startDate", mesEquipmentExamineDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineDTOQuery.getEndDate())) {
            query.put("endDate", mesEquipmentExamineDTOQuery.getEndDate());
        }

        if (mesEquipmentExamineDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesEquipmentExamineDTOQuery.getStopFlag());
        }

        if (mesEquipmentExamineDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesEquipmentExamineDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineDTOQuery.getOrderBy())) {
            query.put("orderBy", mesEquipmentExamineDTOQuery.getOrderBy());
        }

        List<MesEquipmentExamineDTO> list = convert(mesEquipmentExamineService.selectByQuery(query));
        Page<MesEquipmentExamineDTO> page = Page.from(mesEquipmentExamineDTOQuery.getPn(), mesEquipmentExamineDTOQuery.getPageSize(), list);
        page.setTotalItems(mesEquipmentExamineService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesEquipmentExamineService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesEquipmentExamine delete failed.", e);
            throw new RuntimeException("MesEquipmentExamine delete failed, Please order your code.", e);
        }
    }
}